*****************************
*** GENDER GAP ANALYSIS *****
*** QUINN M. ALBAUGH ********
*** NOVEMBER 2023 VERSION ***
*****************************

*** SET WORKING DIRECTORY
clear all

*** CHANGE SETTINGS FOR GRAPHS
set more off, permanently
grstyle init
grstyle set plain, horizontal nogrid
graph set window fontface "Helvetica"

*** LOAD MULTIPLY IMPUTED DATASET
/*
use "${here}MICE2.dta", clear

keep genderid trans sexualid4 education income born_outside_canada indigenous racialized mother_tongue4 ///
	partyid voteintention votechoice ///
	immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy international_trade government_intervention income_inequality wealth_gap ///
	age4 pt user_language cps21_weight_general_all ///
	_mi _mj

mi import ice, automatic clear

*** GENERATE LEFT-RIGHT SCALE
gen left = (immigration + refugees + jobs_vs_environment_pes + family_values + bilingualism + equal_rights + immigrant_integration + immigrant_jobs + abortion + conversion_therapy + government_intervention + income_inequality + wealth_gap) / 13

*** GENERATE VECTOR OF DUMMY VARIABLES FOR PARTY IDENTIFICATION
tab partyid, gen(pid_)

*** GENERATE VECTOR OF DUMMY VARIABLES FOR VOTE CHOICE
tab votechoice, gen(vote_)

*** RECODE INCOME
recode income (1=2)
gen income7 = income - 1
label variable income7 "Income"
*/

****************************************************
*** DROPPING AND REPLACING NONBINARY RESPONDENTS ***
****************************************************

/*
forval i = 1/30 {
	forval j = 1/100 {
		set seed `j'
		preserve
		keep if _mi_m == 0
		keep if genderid == 3
		gen random = runiform()
		sort random
		gen Drop = 1 if _n <= `i'
		keep if Drop == 1
		keep _mi_id Drop
		save DroppingNB_`i'_`j'.dta, replace
		restore
		
		preserve
		keep if _mi_m == 0
		keep if genderid < 3 & trans ~= 1
		gen random = runiform()
		sort random
		gen Replace = 1 if _n <= `i'
		keep if Replace == 1
		keep _mi_id Replace
		save ReplacingNB_`i'_`j'.dta, replace
		restore
		
		preserve
		merge m:1 _mi_id using DroppingNB_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNB_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit pid_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using LiberalPartyID_DropReplaceNB_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/30 {
	forval j = 1/100 {
		set seed `j'
		
		preserve
		merge m:1 _mi_id using DroppingNB_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNB_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit pid_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using ConservativePartyID_DropReplaceNB_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/30 {
	forval j = 1/100 {
		set seed `j'
		
		preserve
		merge m:1 _mi_id using DroppingNB_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNB_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit pid_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using NDPPartyID_DropReplaceNB_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/30 {
	forval j = 1/100 {
		set seed `j'
		
		preserve
		merge m:1 _mi_id using DroppingNB_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNB_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit vote_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using LiberalVote_DropReplaceNB_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}
*/


********************************************************
*** DROPPING AND DREPLACING NONBINARY-DK RESPONDENTS ***
********************************************************

/*
forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
		preserve
		keep if _mi_m == 0
		keep if genderid == 3 & trans == 2
		gen random = runiform()
		sort random
		gen Drop = 1 if _n <= `i'
		keep if Drop == 1
		keep _mi_id Drop
		save DroppingNBDK_`i'_`j'.dta, replace
		restore
		
		preserve
		keep if _mi_m == 0
		keep if genderid < 3 & trans ~= 1
		gen random = runiform()
		sort random
		gen Replace = 1 if _n <= `i'
		keep if Replace == 1
		keep _mi_id Replace
		save ReplacingNBDK_`i'_`j'.dta, replace
		restore
		
		preserve
		merge m:1 _mi_id using DroppingNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNBDK_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit pid_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using LiberalPartyID_DropReplaceNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
		
		preserve
		merge m:1 _mi_id using DroppingNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNBDK_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit pid_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using ConservativePartyID_DropReplaceNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
		
		preserve
		merge m:1 _mi_id using DroppingNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNBDK_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit pid_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using NDPPartyID_DropReplaceNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
		
		preserve
		merge m:1 _mi_id using DroppingNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
		
		merge m:1 _mi_id using ReplacingNBDK_`i'_`j'.dta
		replace genderid = 3 if Replace == 1
		drop _merge Replace
		
		mi estimate: logit vote_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using LiberalVote_DropReplaceNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}
*/




use LiberalPartyID_Baseline.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using LiberalPartyID_DropReplaceNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Party Identification" "Men-Nonbinary People", size(medsmall))
graph save LiberalPID_DropReplaceNBDK_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalPID_DropReplaceNBDK_MNB_Sig.gph, replace

graph twoway line Gap3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Party Identification" "Women-Nonbinary People", size(medsmall))
graph save LiberalPID_DropReplaceNBDK_WNB_Gap.gph, replace

graph twoway line Sig3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalPID_DropReplaceNBDK_WNB_Sig.gph, replace

graph combine LiberalPID_DropReplaceNBDK_MNB_Gap.gph LiberalPID_DropReplaceNBDK_WNB_Gap.gph LiberalPID_DropReplaceNBDK_MNB_Sig.gph LiberalPID_DropReplaceNBDK_WNB_Sig.gph, col(2)
graph export "LiberalPartyIDDropReplaceNBDK.png", height(800) width(800) replace

use ConservativePartyID_DropReplaceNBDK_1_1.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using ConservativePartyID_DropReplaceNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Conservative Party Identification" "Men-Nonbinary People", size(medsmall))
graph save ConservativePID_DropReplaceNBDK_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations" " "  "(p < 0.1)") ///
	title(" ", size(medsmall))
graph save ConservativePID_DropReplaceNBDK_MNB_Sig.gph, replace

graph combine ConservativePID_DropReplaceNBDK_MNB_Gap.gph ConservativePID_DropReplaceNBDK_MNB_Sig.gph, col(1)
graph export "ConservativePartyIDDropReplaceNBDK.png", height(800) width(800) replace

use NDPPartyID_DropReplaceNBDK_1_1.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using NDPPartyID_DropReplaceNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(0 0.2)) ///
	ylabel(0(0.05)0.2) ///
	ytitle("Mean Estimate of Gap") ///
	title("NDP Party Identification" "Men-Nonbinary People", size(medsmall))
graph save NDPPID_DropReplaceNBDK_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save NDPPID_DropReplaceNBDK_MNB_Sig.gph, replace

graph combine NDPPID_DropReplaceNBDK_MNB_Gap.gph NDPPID_DropReplaceNBDK_MNB_Sig.gph, col(1)
graph export "NDPPartyIDDropReplaceNBDK.png", height(800) width(800) replace

use LiberalVote_DropReplaceNBDK_1_1.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using LiberalVote_DropReplaceNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Liberal Voting" "Women-Nonbinary People") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Voting" "Women-Nonbinary People", size(medsmall))
graph save LiberalVote_DropReplaceNBDK_WNB_Gap.gph, replace

graph twoway line Sig3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalVote_DropReplaceNBDK_WNB_Sig.gph, replace

graph combine LiberalVote_DropReplaceNBDK_WNB_Gap.gph LiberalVote_DropReplaceNBDK_WNB_Sig.gph, col(1)
graph export "LiberalVoteDropReplaceNBDK.png", height(800) width(800) replace

use LiberalPartyID_Baseline.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using LiberalPartyID_DropNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Party Identification" "Men-Nonbinary People", size(medsmall))
graph save LiberalPID_DropNBDK_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalPID_DropNBDK_MNB_Sig.gph, replace

graph twoway line Gap3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Party Identification" "Women-Nonbinary People", size(medsmall))
graph save LiberalPID_DropNBDK_WNB_Gap.gph, replace

graph twoway line Sig3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalPID_DropNBDK_WNB_Sig.gph, replace

graph combine LiberalPID_DropNBDK_MNB_Gap.gph LiberalPID_DropNBDK_WNB_Gap.gph LiberalPID_DropNBDK_MNB_Sig.gph LiberalPID_DropNBDK_WNB_Sig.gph, col(2)
graph export "LiberalPartyIDDropNBDK.png", height(800) width(800) replace

use ConservativePartyID_DropNBDK_1_1.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using ConservativePartyID_DropNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Conservative Party Identification" "Men-Nonbinary People", size(medsmall))
graph save ConservativePID_DropNBDK_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations" " "  "(p < 0.1)") ///
	title(" ", size(medsmall))
graph save ConservativePID_DropNBDK_MNB_Sig.gph, replace

graph combine ConservativePID_DropNBDK_MNB_Gap.gph ConservativePID_DropNBDK_MNB_Sig.gph, col(1)
graph export "ConservativePartyIDDropNBDK.png", height(800) width(800) replace

use NDPPartyID_DropNBDK_1_1.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using NDPPartyID_DropNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle(" ") ///
	yscale(range(0 0.2)) ///
	ylabel(0(0.05)0.2) ///
	ytitle("Mean Estimate of Gap") ///
	title("NDP Party Identification" "Men-Nonbinary People", size(medsmall))
graph save NDPPID_DropNBDK_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save NDPPID_DropNBDK_MNB_Sig.gph, replace

graph combine NDPPID_DropNBDK_MNB_Gap.gph NDPPID_DropNBDK_MNB_Sig.gph, col(1)
graph export "NDPPartyIDDropNBDK.png", height(800) width(800) replace

use LiberalVote_DropNBDK_1_1.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using LiberalVote_DropNBDK_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Liberal Voting" "Women-Nonbinary People") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Voting" "Women-Nonbinary People", size(medsmall))
graph save LiberalVote_DropNBDK_WNB_Gap.gph, replace

graph twoway line Sig3 Number, ///
	lcolor(black) ///
	xscale(range(1 18)) ///
	xlabel(1 18) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalVote_DropNBDK_WNB_Sig.gph, replace

graph combine LiberalVote_DropNBDK_WNB_Gap.gph LiberalVote_DropNBDK_WNB_Sig.gph, col(1)
graph export "LiberalVoteDropNBDK.png", height(800) width(800) replace
*/

use LiberalPartyID_DropReplaceNB_1_1.dta, clear
forval i = 1/30 {
	forval j = 1/100 {
		append using LiberalPartyID_DropReplaceNB_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle(" ") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Party Identification" "Men-Nonbinary People", size(medsmall))
graph save LiberalPID_DropReplace_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle(" ") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalPID_DropReplace_MNB_Sig.gph, replace

graph twoway line Gap3 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Party Identification" "Women-Nonbinary People", size(medsmall))
graph save LiberalPID_DropReplace_WNB_Gap.gph, replace

graph twoway line Sig3 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalPID_DropReplace_WNB_Sig.gph, replace

graph combine LiberalPID_DropReplace_MNB_Gap.gph LiberalPID_DropReplace_WNB_Gap.gph LiberalPID_DropReplace_MNB_Sig.gph LiberalPID_DropReplace_WNB_Sig.gph, col(2)
graph export "LiberalPartyIDDropReplace.png", height(800) width(800) replace

use ConservativePartyID_DropReplaceNB_1_1.dta, clear
forval i = 1/30 {
	forval j = 1/100 {
		append using ConservativePartyID_DropReplaceNB_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle(" ") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Conservative Party Identification" "Men-Nonbinary People", size(medsmall))
graph save ConservativePID_DropReplace_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations" " "  "(p < 0.1)") ///
	title(" ", size(medsmall))
graph save ConservativePID_DropReplace_MNB_Sig.gph, replace

graph combine ConservativePID_DropReplace_MNB_Gap.gph ConservativePID_DropReplace_MNB_Sig.gph, col(1)
graph export "ConservativePartyIDDropReplace.png", height(800) width(800) replace

use NDPPartyID_DropReplaceNB_1_1.dta, clear
forval i = 1/30 {
	forval j = 1/100 {
		append using NDPPartyID_DropReplaceNB_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap2 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle(" ") ///
	yscale(range(0 0.2)) ///
	ylabel(0(0.05)0.2) ///
	ytitle("Mean Estimate of Gap") ///
	title("NDP Party Identification" "Men-Nonbinary People", size(medsmall))
graph save NDPPID_DropReplace_MNB_Gap.gph, replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save NDPPID_DropReplace_MNB_Sig.gph, replace

graph combine NDPPID_DropReplace_MNB_Gap.gph NDPPID_DropReplace_MNB_Sig.gph, col(1)
graph export "NDPPartyIDDropReplace.png", height(800) width(800) replace

use LiberalVote_DropReplaceNB_1_1.dta, clear
forval i = 1/30 {
	forval j = 1/100 {
		append using LiberalVote_DropReplaceNB_`i'_`j'.dta
	}
}
drop in 1

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

collapse (mean) Gap2 Gap3 (sum) Sig2 Sig3, by(Number)

graph twoway line Gap3 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle("Liberal Voting" "Women-Nonbinary People") ///
	yscale(range(-0.2 0)) ///
	ylabel(-.2(0.05)0) ///
	ytitle("Mean Estimate of Gap") ///
	title("Liberal Voting" "Women-Nonbinary People", size(medsmall))
graph save LiberalVote_DropReplace_WNB_Gap.gph, replace

graph twoway line Sig3 Number, ///
	lcolor(black) ///
	xscale(range(1 30)) ///
	xlabel(1 30) ///
	xtitle("Number of Nonbinary-Don't Know" "Respondents Dropped") ///
	yscale(range(0 100)) ///
	ylabel(0(25)100) ///
	ytitle("Number of Simulations (p < 0.1)") ///
	title(" ", size(medsmall))
graph save LiberalVote_DropReplace_WNB_Sig.gph, replace

graph combine LiberalVote_DropReplace_WNB_Gap.gph LiberalVote_DropReplace_WNB_Sig.gph, col(1)
graph export "LiberalVoteDropReplace.png", height(800) width(800) replace

